import java.io.FileReader ;
import java.io.FileWriter ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.PrintWriter;

public class Fichier
{	
	public static void save(String filename, Grid g) 
	{
		PrintWriter wr;
		try
		{
			boolean find = false;
			wr = new PrintWriter(new FileWriter(filename));
			for(int i=P4.HEIGHT-1 ; i>=0 && find == false; i--)
			{
				for(int j=0 ; j<P4.WIDTH && find == false; j++)
				{
					if(g.getGrid(i, j) == P4.PLAYER1)
						wr.print("X");
					else if(g.getGrid(i, j) == P4.PLAYER2)
						wr.print("O");
					else
						wr.print(" ");
				}
				wr.println("");
			}
			wr.flush();
			wr.close();
		}
		catch (NullPointerException a)
		{
			System.out.println("Erreur : pointeur null");
		}
		catch (IOException a)
		{
			System.out.println("Problème d'IO");
		}
	}

	public static void load(String filename, Grid g, Int pos) 
	{
		BufferedReader rd;
		String tmp = "";
		pos.setVal(-1);
		try
		{
			rd = new BufferedReader(new FileReader(filename)) ;
			int x = 0;
			int y = P4.HEIGHT-1;
			boolean find = false;
			while (rd.ready()==true) 
			{
				tmp = rd.readLine();
				for(int i=0; i<tmp.length() && find == false; i++)
				{
					if(tmp.charAt(i) == 'X' && g.getGrid(y, x) == P4.EMPTY)
					{
						pos.setVal(x);
						find = true;
					}
					else if(tmp.charAt(i) == 'O' && g.getGrid(y, x) == P4.EMPTY)
					{
						pos.setVal(x);
						find = true;
					}
					x++;
				}
				x = 0;
				y--;
			}
			rd.close();
		}
		catch (NullPointerException a)
		{
			System.out.println("Erreur : pointeur null");
		}
		catch (IOException a) 
		{
			System.out.println("Problème d'IO");
		}
	}
}